package controllers.managers
{
	import flash.net.SharedObject;
	
	import mx.logging.ILogger;
	
	import utils.LogUtil;

	/**
	 *  本地缓存管理
	 *  
	 **/
	public class DataStoreManager
	{
		public static var NAME : String = "DataStoreManager";
		public static var STORE_AREA : String = "STORE_AREA";

		
		/*本地存储*/
		private var sharedObj:SharedObject;
		private var logger:ILogger;
		private var memCache:Object = null;
		private var isUseMemCache:Boolean = false;
		
		public function DataStoreManager()
		{
			logger = LogUtil.getInst().getLogger(this);
			initLocalDataStore();
		}

		
		private function initLocalDataStore():void
		{
			sharedObj = SharedObject.getLocal(STORE_AREA);
			if ( sharedObj == null )
			{				
				isUseMemCache = true;
				memCache = new Object();
				logger.error("获取ShareObject失败，改用内存缓存，如果浏览器关闭后这些缓存值将失效。");
			}
		}
		
		public function set(key:String, value:Object):void
		{
			if ( isUseMemCache ){
				memCache[key] = value;
			}else
			{
				sharedObj.data[key] = value; 
				sharedObj.flush();
			}
		}
		
		public function clear():void
		{
			if ( isUseMemCache ){
				memCache = {};
			}else
			{
				sharedObj.clear();				
			}
		}
		
		public function get(key:String):Object
		{
			if ( isUseMemCache ){
				return memCache[key];
			}else
			{
				return sharedObj.data[key];
			}
		}
	}
}